package server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import com.mongodb.DBObject;

public class PostCustomUrl {
	public static String post(String title,String description,ArrayList<String> tag,DBObject users,String cate)
	{
		String str = "-1";
		try {
			String keyword = "";
		    
			if(!cate.equals(""))
			{
				keyword=cate;
			} else {
			    if(tag.size() > 0)
			    {
			    	for (int i = 0; i < tag.size(); i++) {
			    		if(keyword.equals(""))
			    		{
			    			keyword = tag.get(i);
			    		} else {
			    			keyword += ","+tag.get(i);
			    		}
			    			
					}
			    }
			}
			
			
			URL url = new URL(users.get("blogurl").toString());
			String user = users.get("usernameblog").toString();
		    String password = users.get("passwordblog").toString();
			
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoOutput(true);
	        connection.setRequestMethod("POST");
	        
	        connection.setRequestProperty("Accept-Charset", "UTF-8");
	        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	        String data = "post[title]=" + URLEncoder.encode(title,"utf-8")+"&post[content]="+URLEncoder.encode(description,"utf-8")+"&post[tag]=" + URLEncoder.encode(keyword,"utf-8")
	        +"&post[user]=" + user+"&password=" + password;
	        connection.setRequestProperty("Content-Length", data.length()+"");
	        
	        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"utf-8");
	        
	        writer.write(data);
	        writer.close();
	
	        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        	 String content = "";
            	 char[] buf = new char[102102];
         		 int numRead=0;
                 while((numRead=reader.read(buf)) != -1){
                     String readData = String.valueOf(buf, 0, numRead);
                     content += readData;
                     buf = new char[102102];
                 }
                 reader.close();
                 str = content.replaceAll("\\{\"id\":", "").replaceAll("\\}", "");
	        } else {
	            // Server returned HTTP error code.
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	public static void main(String[] args) {
//		post("user","","http://picmul.herokuapp.com/posts");
		System.out.println(HTMLEntities.htmlentities("http://picmul.herokuapp.com/posts?asdada&asdasdas"));
		try {
			System.out.println(URLEncoder.encode("http://picmul.herokuapp.com/posts?asdada&asdasdas","utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
